package MTT::Controller::Login;
use Moose;
use namespace::autoclean;

BEGIN {extends 'Catalyst::Controller'; }

=head1 NAME

MTT::Controller::Login - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut


=head2 index

=cut

use utf8;
use MTT::Form::Login;

sub index :Path :Args(0) {
    my ( $self, $c ) = @_;

	if ( $c->req->method eq 'POST' ) {
		my $username = $c->req->param('username');
		my $password = $c->req->param('password');

		if ($username && $password) {
			if ( $c->authenticate({
						username => $username,
						password => $password }) ) {
				$c->response->redirect( $c->uri_for('/') );
				return;
			}
			else {
				$c->stash(error_msg => 'Неправильное имя пользователя или пароль');
			}
		}
		else {
			$c->stash(error_msg => 'Неправильное имя пользователя или пароль');
		}

		$c->stash(
			form => MTT::Form::Login->new(),
			template => 'login.tt2'
		);
	}
	else {

		$c->stash(
			form => MTT::Form::Login->new(),
			template => 'login.tt2'
		);
	}

}


=head1 AUTHOR

A clever guy

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

__PACKAGE__->meta->make_immutable;

1;
